package com.lw.sbdemo2.web;

import com.lw.sbdemo2.common.base.BaseController;
import com.lw.sbdemo2.common.exception.ApiException;
import com.lw.sbdemo2.common.utils.IdUtils;
import com.lw.sbdemo2.common.utils.ValidUtil;
import com.lw.sbdemo2.common.vo.PageInfo;
import com.lw.sbdemo2.common.web.anno.Required;
import com.lw.sbdemo2.common.web.anno.ResultController;
import com.lw.sbdemo2.entity.SxEmployeeInfo;
import com.lw.sbdemo2.service.SxEmployeeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>标题: 管理员信息表服务 - 请求控制层</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 山人行工作室</p>
 *
 * @version: 1.0
 * @author: LinWang
 * @date 2018-11-29
 */
@ResultController
@Api(description = "SxEmployeeInfoController", tags = {"管理员信息表服务"})
@RequestMapping(value = "/employee")
@Slf4j
public class SxEmployeeInfoController extends BaseController {

    /**
     * 管理员信息表服务
     */
    @Autowired
    private SxEmployeeInfoService sxEmployeeInfoService;

    public static void main(String[] args) {
        List<SxEmployeeInfo> dataList = new ArrayList<>();
        dataList.add(null);

        Map<String,Object> map = null;
        String itemName = "";
        if(map == null || ValidUtil.isEmptyOrNull(map.get(itemName))){
            System.out.println("1234");
        } else {
            System.out.println("123");
        }

    }

    @ApiOperation(value = "管理员登录逻辑")
    @PostMapping("/login")
    public SxEmployeeInfo login(SxEmployeeInfo sxEmployeeInfo, HttpServletRequest request) {

        SxEmployeeInfo sxEmployeeInfoDb = sxEmployeeInfoService.get(sxEmployeeInfo);
        if (sxEmployeeInfoDb == null) {
            throw new ApiException("登录失败，该用户不存在~");
        } else {
            if (!sxEmployeeInfoDb.getEmpPw().equals(sxEmployeeInfo.getEmpPw())) {
                throw new ApiException("登录失败，密码错误~");
            }
            HttpSession session = request.getSession();
            session.setAttribute("userId", sxEmployeeInfo.getEmpId());
            return sxEmployeeInfoDb;
        }
    }

    @ApiOperation(value = "管理员注销退出")
    @PostMapping("/logout")
    public void logout(@RequestHeader String operator, HttpServletRequest request) {
        log.info("员工注销退出，userId：{}", operator);
        HttpSession session = request.getSession();
        session.setAttribute("userId", "");
    }

    @ApiOperation(value = "获取管理员信息表列表")
    @GetMapping("")
    public List<SxEmployeeInfo> findList(SxEmployeeInfo sxEmployeeInfo) {
        return sxEmployeeInfoService.findList(sxEmployeeInfo);
    }

    @ApiOperation(value = "获取管理员信息表详细信息", notes = "根据url的id来获取管理员信息表详细信息")
    @GetMapping("/{id}")
    public SxEmployeeInfo get(@PathVariable String id) {
        return sxEmployeeInfoService.getById(id);
    }

    @ApiOperation(value = "获取管理员信息表列表（分页）", response = PageInfo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "关键词", dataType = "string"),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int"),
    })
    @GetMapping(value = "/page")
    public PageInfo<SxEmployeeInfo> page(String query, @Required("pageNum") PageInfo pageInfo, SxEmployeeInfo sxEmployeeInfo) {
        return sxEmployeeInfoService.findListPage(query, pageInfo, sxEmployeeInfo);
    }

    @ApiOperation(value = "创建管理员信息表")
    @PostMapping("/insert")
    public void insert(SxEmployeeInfo sxEmployeeInfo) {
        sxEmployeeInfo.setCreatedTime(new Date());
        sxEmployeeInfo.setEmpId(IdUtils.uuid());
        sxEmployeeInfoService.insert(sxEmployeeInfo);
    }

    @ApiOperation(value = "更新管理员信息表详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员信息表ID", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/update")
    public void update(SxEmployeeInfo sxEmployeeInfo) {
        sxEmployeeInfo.setModifiedTime(new Date());
        sxEmployeeInfoService.update(sxEmployeeInfo);
    }

    @ApiOperation(value = "删除管理员信息表", notes = "删除管理员信息表")
    @ApiImplicitParam(name = "id", value = "管理员信息表ID", required = true, paramType = "query", dataType = "String")
    @PostMapping("/delete")
    public void deleteSxEmployeeInfo(SxEmployeeInfo sxEmployeeInfo) {

        sxEmployeeInfoService.delete(sxEmployeeInfo);
    }

}
